#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <sys/stat.h>
#include <sys/types.h>


void cmd_cp(char *comm)
{
	char *argv[4];
	char *s;
	int nr=0, ch, i=0;
	int f,g;
	argv[i] = strtok (comm, " ");
	nr++; i++;
	while ((argv[i++] = strtok (NULL, " ")) != NULL)
		nr++;
	if (nr < 3)
	{
		fprintf (stderr, "-invalid command!\n");
                fprintf (stderr, "-usage: cp file1 file2\n");
	}
	else {
		f = open (argv[1], O_RDONLY);
		g = creat (argv[2], O_WRONLY);
		while ((ch = read (f, s, 256)) > 0) {
			fputs (s, stderr);
			fprintf (stderr, "\n");
			write (g, s, 256); }
		close (f);
		close (g);
	}
}

int main()
{
        char *comm = readline ("usage: cp file\ncommand: ");
	cmd_cp(comm);
	return 0;
}
